package csu.web.credit_bank.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import csu.web.credit_bank.mapper.ResourceRecommendationsMapper;
import csu.web.credit_bank.pojo.ResourceRecommendation;
import csu.web.credit_bank.service.ResourceRecommendationsService;
import csu.web.credit_bank.utils.UUIDManager;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ResourceRecommendationsServiceImpl extends ServiceImpl<ResourceRecommendationsMapper, ResourceRecommendation> implements ResourceRecommendationsService {

    @Autowired
    private UUIDManager uuidManager;

    @Override
    @Transactional
    public ResourceRecommendation logResourceView(String userId, String major) {
        // 1. 构建查询条件，查找是否已存在记录
        QueryWrapper<ResourceRecommendation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("major", major);

        ResourceRecommendation existingRecommendation = this.getOne(queryWrapper);

        if (existingRecommendation != null) {
            // 如果记录已存在，则将其 view_count + 1
            existingRecommendation.setViewCount(existingRecommendation.getViewCount() + 1);
            boolean success = this.updateById(existingRecommendation);
            if (!success) {
                throw new RuntimeException("更新浏览记录失败");
            }
            return existingRecommendation;
        } else {
            ResourceRecommendation newRecommendation = new ResourceRecommendation();
            String recordId = uuidManager.getUUID(); // 从ID池获取新ID
            if (recordId == null) {
                throw new RuntimeException("ID池已空，无法创建推荐记录");
            }
            newRecommendation.setId(recordId);
            newRecommendation.setUserId(userId);
            newRecommendation.setMajor(major);
            // view_count 字段在数据库层面已设置默认值为1，此处也可以显式设置
            newRecommendation.setViewCount(1);

            boolean success = this.save(newRecommendation);
            if (!success) {
                throw new RuntimeException("保存新的浏览记录失败");
            }
            return newRecommendation;
        }
    }
    // --- Standard CRUD Implementation ---

    @Override
    public ResourceRecommendation createRecommendation(ResourceRecommendation recommendation) {
        String id = uuidManager.getUUID();
        if (id == null) {
            throw new RuntimeException("ID池已空，无法创建推荐记录.");
        }
        recommendation.setId(id);
        // 通常新创建的浏览记录，其浏览量应为1
        if (recommendation.getViewCount() == null) {
            recommendation.setViewCount(1);
        }
        this.save(recommendation);
        return recommendation;
    }

    @Override
    public ResourceRecommendation getRecommendation(String userId, String major) {
        QueryWrapper<ResourceRecommendation> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId).eq("major", major);
        return this.getOne(wrapper);
    }

    @Override
    public List<ResourceRecommendation> getRecommendationsByUserId(String userId) {
        QueryWrapper<ResourceRecommendation> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        return this.list(wrapper);
    }

    @Override
    public ResourceRecommendation updateRecommendation(ResourceRecommendation recommendation) {
        this.updateById(recommendation);
        return this.getById(recommendation.getId());
    }

    @Override
    public boolean deleteRecommendation(String id) {
        return this.removeById(id);
    }
}
